// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintAsyncActionBase.h"
#include "Async/AsyncWork.h"
#include "TickTestActor.h"
#include "MyBPAsyncTaskActionBase.generated.h"

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyAsyncTaskNodeResult, int32, Result);
/**
 * 
 */
UCLASS()
class ASYNC_API UMyBPAsyncTaskActionBase : public UBlueprintAsyncActionBase
{
	GENERATED_BODY()

public:
	UPROPERTY(BlueprintAssignable)
	FMyAsyncTaskNodeResult OnSuccess;

	UPROPERTY(BlueprintAssignable)
	FMyAsyncTaskNodeResult OnFail;

	UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject"))
	static UMyBPAsyncTaskActionBase* WaitTickTestActorCounter(UObject* WorldContextObject, ATickTestActor* Actor, int32 TargetCounter);
};

//https://zhuanlan.zhihu.com/p/38881269
class MyAsyncTask : public FNonAbandonableTask
{
	// 1
	friend class FAutoDeleteAsyncTask<MyAsyncTask>;
	UMyBPAsyncTaskActionBase* Node;
	ATickTestActor* Actor;
	int32 TargetCounter;

	MyAsyncTask(UMyBPAsyncTaskActionBase* WaitNode, ATickTestActor* TickTestActor, int32 Target)
		: Node(WaitNode), Actor(TickTestActor), TargetCounter(Target) {}
	// 2
	void DoWork();
	// 3
	FORCEINLINE TStatId GetStatId() const
	{
		RETURN_QUICK_DECLARE_CYCLE_STAT(MyAsyncTask, STATGROUP_ThreadPoolAsyncTasks);
	}
};